package cn.bgw.compare.service;

import cn.bgw.compare.manager.TradeLogManager;
import cn.bgw.compare.mapper.TradelogMapper;
import cn.bgw.compare.model.Rank;
import cn.bgw.compare.model.Tradelog;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class TradelogService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TradeLogManager tradeLogManager;
    @Autowired
    private TradelogMapper tradelogMapper;

    public List<Tradelog> getList() {
        List<Tradelog> datas = tradeLogManager.selectAll();
        datas = datas.stream().filter(log -> {
            String resultParams = log.getResultParams();
            if (resultParams == null) {
                return false;
            }

            Rank rank = JSONObject.parseObject(resultParams, Rank.class);
            if (rank.getRank() == null) {
                return false;
            }

            return true;
        }).sorted(Comparator.comparing((Tradelog log) -> {
            String resultParams = log.getResultParams();
            Rank rank = JSONObject.parseObject(resultParams, Rank.class);
            return rank.getRank();
        }).thenComparing(Tradelog::gettModified, Comparator.reverseOrder())).collect(Collectors.toList());
        logger.info("{}", JSONObject.toJSONString(datas));
        return datas;
    }

    private void insertDatas() {
        Random random = new Random();
        for (int i = 10; i < 20; i++) {
            Tradelog tradelog = new Tradelog();
            tradelog.setTradeid(i + "");
            tradelog.setOperator(i);

            DateTime randomDateTime = randomDateTime("2022-06-10 10:00:00", "2022-06-20 12:00:00");
            tradelog.settModified(randomDateTime.toDate());

            if (i % 2 == 0) {
                tradelog.setResultParams(JSONObject.toJSONString(new Rank(random.nextInt(5), "")));
            }
            tradelogMapper.insert(tradelog);
        }
    }

    /**
     * 在[beginStr,endStr]生成随机日期+时间
     *
     * @param beginStr
     * @param endStr
     * @return
     */
    public static DateTime randomDateTime(String beginStr, String endStr) {
        try {

            DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
            DateTime beginTime = DateTime.parse(beginStr, format);
            DateTime endTime = DateTime.parse(endStr, format);

            if (beginTime.getMillis() > endTime.getMillis()) {
                return null;
            }

            long randDateTime = random(beginTime.getMillis(), endTime.getMillis());
            return new DateTime(randDateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 在[beginStr,endStr]只随机生成时间,不包含日期
     * 在没有添加日期格式时,默认为1970-01-01
     *
     * @param beginStr
     * @param endStr
     * @return
     */
    public static DateTime randomTime(String beginStr, String endStr) {
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
            DateTime beginTime = DateTime.parse(beginStr, format);
            DateTime endTime = DateTime.parse(endStr, format);

            if (beginTime.getMillis() > endTime.getMillis()) {
                return null;
            }

            long randDateTime = random(beginTime.getMillis(), endTime.getMillis());
            return new DateTime(randDateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 随机生成时间
     * 使用Math.random()方法---返回一个在介于(0,1)的随机数
     *
     * @param begin
     * @param end
     * @return
     */
    private static long random(long begin, long end) {
        long rand = begin + (long) (Math.random() * (end - begin));
        if (rand == begin || rand == end) {
            return random(begin, end);
        }
        return rand;
    }
}
